package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.StudentEvaluationScoreJdbc;
import com.wuseguang.springcloud.dao.repository.StudentEvaluationScoreRepository;
import com.wuseguang.springcloud.service.StudentEvaluationScoreService;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 学生评价分数表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-02-02
 */
@Service
public class StudentEvaluationScoreServiceImpl implements StudentEvaluationScoreService {

    public Logger logger = LoggerFactory.getLogger(this.getClass());



    @Autowired
    private StudentEvaluationScoreJdbc studentEvaluationScoreJdbc;
    @Autowired
    private StudentEvaluationScoreRepository studentEvaluationScoreRepository;


    @Override
    public Result findClassEvaluationInformation(String deviceid, Integer findType, String startTime, String endTime) {

        //查询类型的sql
        String typeSql = " and YEARWEEK(date_format(add_time,'%Y-%m-%d')) = YEARWEEK(now()) ";
        //判断查询类型
        if (findType == 1) {
            logger.info("查询上周数据");
            typeSql += " and YEARWEEK(date_format(add_time,'%Y-%m-%d')) = YEARWEEK(now())-1 ";
        } else if (findType == 2) {
            logger.info("查询本月数据");
            typeSql += " and date_format(add_time,'%Y-%m')=date_format(now(),'%Y-%m') ";
        } else if (findType == 3) {
            logger.info("查询自定义数据");
            typeSql += " and date_format(add_time,'%Y-%m-%d')  between date_format('" + startTime + "','%Y-%m-%d') and date_format('" + endTime + "','%Y-%m-%d') ";
        } else {
            logger.info("查询本周数据");
            typeSql += " and YEARWEEK(date_format(add_time,'%Y-%m-%d')) = YEARWEEK(now()) ";
        }

        //返回的集合
        Map<String,Object> resultMap = new HashMap<>();

        //获取查询设备信息
        Map<String,Object> map = studentEvaluationScoreJdbc.queryDevice(deviceid);
        //获取班级id
        String classid = map.get("classid").toString();

        //查询班级的评分记录
        String sql = "select sum(ses.fraction) fractionSum  from student_evaluation_score ses where " +
                "EXISTS(select *  from student s where s.id=ses.student_id and s.class_id=?) "+typeSql+"  GROUP BY sign(ses.fraction) ORDER BY fractionSum desc";
        //获取评分项
        List<Map<String,Object>>  list = studentEvaluationScoreJdbc.queryListMap(sql,classid);
        //应为这个学生没有分数，所以要填写默认分数
        Map<String,Object> classMap = new HashMap<>();
        //判断是否为空
        if(list.isEmpty()){
            classMap.put("positiveScore","0");
            classMap.put("minusScore","0");
        }else if(list.size()==1){
            //判断正负数
            if(Integer.valueOf(list.get(0).get("fractionSum").toString())>0){
                logger.info("正分");
                classMap.put("positiveScore","+"+list.get(0).get("fractionSum").toString());
                classMap.put("minusScore","0");
            }else{
                logger.info("负分");
                classMap.put("positiveScore","0");
                classMap.put("minusScore",list.get(0).get("fractionSum").toString());
            }
        }else{
            classMap.put("positiveScore","+"+list.get(0).get("fractionSum").toString());
            classMap.put("minusScore",list.get(1).get("fractionSum").toString());

        }        //查询个人荣誉榜
        sql = "SELECT  SUM(fraction) fraction,s.student_name,head_portrait FROM student_evaluation_score ses " +
                "left JOIN student s on (s.id = ses.student_id) WHERE s.class_id = ? " +typeSql+
                "  GROUP BY s.id HAVING sign(fraction) > -1 ORDER BY fraction desc LIMIT 15";

        //个人荣誉榜
        List<Map<String,Object>> personalList = studentEvaluationScoreJdbc.queryListMap(sql,classid);


        //赋值并且返回
        resultMap.put("className",map.get("classname"));
        resultMap.put("classIcon",map.get("class_icon"));
        resultMap.put("classMap",classMap);
        resultMap.put("personalList",personalList);
        return new Result(resultMap);
    }
}
